package cn.tyoui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.tyoui.bean.UserBean;
import cn.tyoui.service.UserManageService;

@Controller
public class UserController {

	@Resource
	private UserManageService userManageService;
	

	@ResponseBody
	@RequestMapping("btn_delete_user")
	public Map<String,Object> deleteUser(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		String id = map.get("id");
		System.out.println("id : " + id);
		boolean isSuccess = false;
		if(id != null ) {
			try {
				userManageService.deleteUser(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			isSuccess = true;
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("user_manage")
	public Map<String,Object> queryAllUsers(){
		Map<String,Object> resultMap = new HashMap<>();
		try {
			List<UserBean> users = userManageService.queryAllUser();
			resultMap.put("users", users);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", true);
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("change_mima")
	public Map<String,Object> changeMima(@RequestBody UserBean user,HttpServletRequest request){
		System.out.println("change_mima");
		System.out.println(user);
		Map<String,Object> resultMap = new HashMap<>();
		if(user != null || user.getUserName() == null || user.getPassWord() == null || user.getAddress() == null || user.getTel() == null) {
			if(user.getUserType() == null) {
				user.setUserType("1");
			}
			try {
				userManageService.updateUser(user);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//if(user.getUserType() == null) {
			HttpSession session = request.getSession();
			session.removeAttribute("user");
		//}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", true);
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("user_register")
	public Map<String,Object> addUser(@RequestBody UserBean user,HttpServletRequest request){
		System.out.println("user_register");
		System.out.println(user);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(user != null || user.getUserName() == null || user.getPassWord() == null || user.getAddress() == null || user.getTel() == null) {
			if(user.getUserType() == null) {
				user.setUserType("1");
			}
			try {
				List<UserBean> users = userManageService.queryOneUser(user);
				System.out.println(users.size());
				if(users.size() == 0) {
					isSuccess = userManageService.addUser(user);
				}
				if(isSuccess) {
					if(user.getUserType() == null) {
						HttpSession session = request.getSession();
						session.setAttribute("user", user);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("login_to")
	public Map<String,Object> loginTo(@RequestBody UserBean user,HttpServletRequest request){
		System.out.println("login_to");
		System.out.println(user);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess ;
		if(user != null || user.getUserName() == null || user.getPassWord() == null || user.getAddress() == null || user.getTel() == null) {
			HttpSession session = request.getSession();
			session.removeAttribute("user");
			if(user.getUserName().equals("admin") && user.getPassWord().equals("admin")) {
				user.setUserType("0");
				isSuccess = true;
				session.setAttribute("user", user);
				resultMap.put("isSuccess", isSuccess);
				resultMap.put("view", "home");
			}else {
				try {
					List<UserBean> users = userManageService.queryOneUser(user);
					System.out.println(users.size());
					if(users.size() != 0) {
						isSuccess = true;
						session.setAttribute("user", users.get(0));
						System.err.println(session.getAttribute("user"));
						resultMap.put("isSuccess", isSuccess);
					}
					resultMap.put("view", "login");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return resultMap;
	}
	@RequestMapping("loyout")
	public void loyout(HttpServletRequest request) {
		System.out.println("loyout");
		HttpSession session = request.getSession();
		System.err.println(session.getAttribute("user"));
		session.removeAttribute("user");
	}
}
